﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using HOOMT.Util;
using HOOMT.CodeGenerator;

namespace HOOMT
{
    public partial class MainForm : Form
    {
        #region 属性
        MainFormDAO mdbo = new MainFormDAO();
        private ObjectModelForm objectForm = null;//new ObjectModelForm();
        //private StateTransitionModelForm stateForm = null;
        //private InformationFlowModelForm informationForm = null;
        //Point modelFormLocation;
        public MainForm mainForm;
        public Diagram currentDiagramFlag = Diagram.NullObject;
        public String id;

        public String projectName;

        public List<OriginalModelForm> modelFormList = new List<OriginalModelForm>();//存储所有绘制载体
        //public List<List<OriginalDiagram>> diagramList_List = new List<List<OriginalDiagram>>();//存储绘制载体的对象链表
        #endregion

        public MainForm()
        {
            InitializeComponent();
            this.id = System.Guid.NewGuid().ToString();
            mainForm = this;
            //modelFormLocation = new Point(this.Location.X + 50, this.Location.Y + 20);
            SystemVar.mainForm = this;
        }

        #region MainForm初始化
        private void MainForm_Load(object sender, EventArgs e)
        {
            //初始化ToolStrip
            this.splitContainer3.Panel1.Controls.Add(this.stateTransitionsModelToolStrip);
            this.splitContainer3.Panel1.Controls.Add(this.informationFlowModelToolStrip);
            this.splitContainer3.Panel1.Controls.Remove(this.stateTransitionsModelToolStrip);
            this.splitContainer3.Panel1.Controls.Remove(this.informationFlowModelToolStrip);
            this.splitContainer3.Panel1.Controls.Add(this.objectModelToolStrip);

            //treeView.Nodes[0].Expand();
            //treeView.Nodes[0].Nodes[0].Expand();
            //初始化三类Form
            //Object
            objectForm = new ObjectModelForm(mainForm);
            objectForm.Text = "ObjectModelForm: ObjectModelView/ Main";
            //treeView.Nodes[0].Nodes[0].Nodes[0].Tag = objectForm;
            //objectForm.preTreeNode = this.treeView.Nodes[0].Nodes[0];
            modelFormList.Add(objectForm);
            //diagramList_List.Add(objectForm.diagramList);
            //objectForm.formNodeFullPath = "Untitled\\Object model\\Main";
            //objectForm.Text = "ObjectModelForm: ObjectModelView/ Main";
            objectForm.TopLevel = false;
            this.splitContainer3.Panel2.Controls.Add(objectForm);
            //State
            //stateForm = new StateTransitionModelForm(mainForm);
            //stateForm.Text = "StateTransitionModelForm: StateModelView/ Main";
            //treeView.Nodes[0].Nodes[1].Nodes[0].Tag = stateForm;//为节点设置绘制载体
            //stateForm.preTreeNode = this.treeView.Nodes[0].Nodes[1];
            //modelFormList.Add(stateForm);
            //stateForm.TopLevel = false;
            //this.splitContainer3.Panel2.Controls.Add(stateForm);
            //Information
            //informationForm = new InformationFlowModelForm(mainForm);
            //informationForm.Text = "InformationFlowModelForm: InformationModelView/ Main";
            //treeView.Nodes[0].Nodes[2].Nodes[0].Tag = informationForm;
            //informationForm.preTreeNode = this.treeView.Nodes[0].Nodes[2];
            //modelFormList.Add(informationForm);
            //informationForm.TopLevel = false;
            //this.splitContainer3.Panel2.Controls.Add(informationForm);

            //显示objectForm
            objectForm.Show();
            objectForm.Focus();//子窗口获得焦点
        }
        #endregion

        #region objectModelToolStrip按钮响应事件
        private void HighOrderObjectDiagram_Click(object sender, EventArgs e)
        {
            currentDiagramFlag = Diagram.HighOrderObject;
            //this.objectModelToolStrip.Focus();
        }

        private void PrimitiveObjectDiagram_Click(object sender, EventArgs e)
        {
            currentDiagramFlag = Diagram.PrimitiveObject;
            //this.objectModelToolStrip.Focus();
        }

        private void StaticSimpleRelationship_Click(object sender, EventArgs e)
        {
            currentDiagramFlag = Diagram.SimpleRelationship;
            //this.objectModelToolStrip.Focus();
        }

        private void StaticComplexRelationship_Click(object sender, EventArgs e)
        {
            currentDiagramFlag = Diagram.ComplexRelationship;
            //this.objectModelToolStrip.Focus();
        }
        #endregion

        #region stateTransitionsModelToolStrip按钮响应事件

        private void HighStateDiagram_Click(object sender, EventArgs e)
        {
            currentDiagramFlag = Diagram.HighState;
            //this.stateTransitionsModelToolStrip.Focus();

        }

        private void PrimitiveStateDiagram_Click(object sender, EventArgs e)
        {
            currentDiagramFlag = Diagram.PrimitiveState;
            //this.stateTransitionsModelToolStrip.Focus();
        }

        private void StateTransitionFlow_Click(object sender, EventArgs e)
        {
            currentDiagramFlag = Diagram.StateTransitionFlow;
            //this.stateTransitionsModelToolStrip.Focus();
        }

        #endregion

        #region informationFlowModelToolStrip按钮响应事件

        private void ProcessDiagram_Click(object sender, EventArgs e)
        {
            currentDiagramFlag = Diagram.Process;
            //this.stateTransitionsModelToolStrip.Focus();
        }

        private void InformationSourceDiagram_Click(object sender, EventArgs e)
        {
            currentDiagramFlag = Diagram.InformationSource;
            //this.stateTransitionsModelToolStrip.Focus();
        }

        private void InformationFlow_Click(object sender, EventArgs e)
        {
            currentDiagramFlag = Diagram.InformationFlow;
            //this.stateTransitionsModelToolStrip.Focus();
        }

        #endregion

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //这会不会有问题？？？？？？
            this.Dispose();
        }
        //鼠标双击treeView
        private void treeView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (treeView.SelectedNode.Tag is OriginalModelForm)
            {
                OriginalModelForm modelForm = (OriginalModelForm)treeView.SelectedNode.Tag;
                ShowToolStrip(modelForm);
                modelForm.BringToFront();
                modelForm.Show();
                modelForm.Focus();
            }
            //treeView.SelectedNode.Tag.
            #region TrewView鼠标操作
            /*String toolStripKey = treeView.SelectedNode.Name;
            if (!hasProcess && toolStripKey != null)
            {
                switch (toolStripKey)
                {
                    case "objectModelMain":
                        //显示objectModelToolStrip
                        this.splitContainer3.Panel1.Controls.Remove(this.stateTransitionsModelToolStrip);
                        this.splitContainer3.Panel1.Controls.Remove(this.informationFlowModelToolStrip);
                        //objectModelToolStrip.BringToFront();
                        this.splitContainer3.Panel1.Controls.Add(this.objectModelToolStrip);
                        //显示objectForm窗口
                        if (objectForm == null)
                        {
                            objectForm = new ObjectModelForm(mainForm, null);
                            modelFormList.Add(objectForm);
                            objectForm.formNodeFullPath = "Untitled\\Object model\\Main";
                            objectForm.Text = "ObjectModelForm: ObjectModelView/ Main";
                            objectForm.TopLevel = false;
                            this.splitContainer3.Panel2.Controls.Add(objectForm);
                            //objectForm.Visible = true;
                            //objectForm.MdiParent = this;
                            //objectForm.Owner = this;//+++++++++++++++++++++++++++
                            //this.Location = modelFormLocation;
                        }
                        //objectForm.TopMost = true;
                        objectForm.BringToFront();
                        objectForm.Show();
                        objectForm.Focus();
                        break;
                    case "objectModelAssociations":
                        break;
                    case "stateTransitionModelMain":
                        //显示stateTransitionsModelToolStrip
                        this.splitContainer3.Panel1.Controls.Remove(this.objectModelToolStrip);
                        this.splitContainer3.Panel1.Controls.Remove(this.informationFlowModelToolStrip);
                        this.splitContainer3.Panel1.Controls.Add(this.stateTransitionsModelToolStrip);
                        //显示stateForm窗口
                        if (stateForm == null)
                        {
                            stateForm = new StateTransitionModelForm(mainForm, null);
                            modelFormList.Add(stateForm);
                            stateForm.formNodeFullPath = "Untitled\\State transition model\\Main";
                            stateForm.Text = "StateTransitionModelForm: StateModelView/ Main";
                            //stateForm.Owner = this;
                            stateForm.TopLevel = false;
                            //objectForm.MdiParent = this;
                            this.splitContainer3.Panel2.Controls.Add(stateForm);
                            //stateForm.Visible = true;
                        }
                        stateForm.BringToFront();
                        stateForm.Show();
                        stateForm.Focus();
                        break;
                    case "stateTransitionModelAssociations":
                        break;
                    case "informationFlowModelMain":
                        //显示stateTransitionsModelToolStrip
                        this.splitContainer3.Panel1.Controls.Remove(this.stateTransitionsModelToolStrip);
                        this.splitContainer3.Panel1.Controls.Remove(this.objectModelToolStrip);
                        this.splitContainer3.Panel1.Controls.Add(this.informationFlowModelToolStrip);
                        //显示informationForm窗口
                        if (informationForm == null)
                        {
                            informationForm = new InformationFlowModelForm(mainForm, null);
                            modelFormList.Add(informationForm);
                            informationForm.formNodeFullPath = "Untitled\\Information flow model\\Main";
                            informationForm.Text = "InformationFlowModelForm: InformationModelView/ Main";
                            informationForm.TopLevel = false;
                            //informationForm.MdiParent = this;
                            this.splitContainer3.Panel2.Controls.Add(informationForm);
                            //informationForm.BringToFront();
                            //informationForm.Owner = this;
                        }
                        informationForm.BringToFront();
                        informationForm.Show();
                        informationForm.Focus();
                        break;
                    case "informationFlowModelAssociations":
                        break;
                }
            }*/
            #endregion
        }
        //右击菜单打开窗口
        private void openForm_Click(object sender, EventArgs e)
        {
            if (treeView.SelectedNode.Tag is OriginalModelForm)
            {
                OriginalModelForm modelForm = (OriginalModelForm)treeView.SelectedNode.Tag;
                ShowToolStrip(modelForm);
                modelForm.BringToFront();
                modelForm.Show();
                modelForm.Focus();
            }
        }
        //右击菜单关闭窗口
        private void closeForm_Click(object sender, EventArgs e)
        {
            if (treeView.SelectedNode.Tag is OriginalModelForm)
            {
                ((OriginalModelForm)treeView.SelectedNode.Tag).Close();
            }
        }
        //显示ToolTrip函数
        private void ShowToolStrip(OriginalModelForm modelForm)
        {
            if (modelForm is ObjectModelForm)
            {
                this.splitContainer3.Panel1.Controls.Remove(this.stateTransitionsModelToolStrip);
                this.splitContainer3.Panel1.Controls.Remove(this.informationFlowModelToolStrip);
                this.splitContainer3.Panel1.Controls.Add(this.objectModelToolStrip);
            }
            else if (modelForm is StateTransitionModelForm)
            {
                this.splitContainer3.Panel1.Controls.Remove(this.informationFlowModelToolStrip);
                this.splitContainer3.Panel1.Controls.Remove(this.objectModelToolStrip);
                this.splitContainer3.Panel1.Controls.Add(this.stateTransitionsModelToolStrip);
            }
            else if (modelForm is InformationFlowModelForm)
            {
                this.splitContainer3.Panel1.Controls.Remove(this.objectModelToolStrip);
                this.splitContainer3.Panel1.Controls.Remove(this.stateTransitionsModelToolStrip);
                this.splitContainer3.Panel1.Controls.Add(this.informationFlowModelToolStrip);
            }
        }
        //实现右键选中Node
        private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeView tn = (TreeView)sender;
            if (e.Button == MouseButtons.Right)
            {
                tn.SelectedNode = e.Node;
            }
        }

        private void deleteNode_Click(object sender, EventArgs e)
        {
            OriginalDiagram diag = (OriginalDiagram)treeView.SelectedNode.Tag;
            diag.Remove();
            treeView.SelectedNode.Remove();
        }

        private void TestConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TestDAO test1 = new TestDAO();
            test1.TestConn();
        }

        private void 测试HOODToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HighOrderObjectDiagramDAO dbo = new HighOrderObjectDiagramDAO();
            dbo.ShowAllInConsole();
        }

        private void saveToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            FrmInputProName fipn = new FrmInputProName();
            fipn.ShowDialog();
            this.Name = fipn.projectName;
            if (fipn.Visible == false)
            {
                fipn.Close();

                MainFormDAO mdbo = new MainFormDAO();
                //          Console.WriteLine(this.Name);
                mdbo.SaveMainForm(this);
                //      Console.WriteLine("save ok");
            }

        }

        private void findbyidToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HighOrderObjectDiagramDAO dbo = new HighOrderObjectDiagramDAO();
            HighOrderObjectDiagram hood = dbo.GetHOODById("3");
            //      Console.WriteLine(hood.Name);
        }

        private void saveFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OriginalModelFormDAO dbo = new OriginalModelFormDAO();

        }

        private void testsubstringToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OriginalModelFormDAO dbo = new OriginalModelFormDAO();
            // Console.WriteLine(dbo.GetSimpleType(dbo));
        }

        private void testfansheToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OriginalModelFormDAO dbo = new OriginalModelFormDAO();
            String type = dbo.GetType().ToString();
            //        Console.WriteLine(type);
            object obj = Assembly.Load("HOOMT").CreateInstance(type);
            //     Console.WriteLine(obj.GetType().ToString());
        }
        //cleanup
        private void cleanUPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MainFormDAO dbo = new MainFormDAO();
            dbo.CleanUp();
        }

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {


            MainFormDAO dbo = new MainFormDAO();
            dbo.GetMainFormById("da313f98-aa7c-4b65-93ac-9e3587922d04", this);
        }

        private void aaaaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmLoadProject frm = new FrmLoadProject();
            frm.ShowDialog();
        }
        //save
        private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (this.projectName == null)
            {
                FrmInputProName fipn = new FrmInputProName();
                //   Console.WriteLine("projectName is " + this.projectName);


                fipn.projectName = this.projectName;
                fipn.ShowDialog();
                this.projectName = fipn.projectName;
                if (fipn.Visible == false)
                {
                    fipn.Close();

                    MainFormDAO mdbo = new MainFormDAO();

                    mdbo.SaveMainForm(this);
                    Console.WriteLine("save ok");
                }
            }
            else
            {
                MainFormDAO mdbo = new MainFormDAO();

                mdbo.SaveMainForm(this);
            }
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmLoadProject frm = new FrmLoadProject();
            frm.ShowDialog();
            if (frm.Visible == false)
            {
                this.id = frm.projectId;
                MainFormDAO dbo = new MainFormDAO();
                dbo.GetMainFormById(frm.projectId, this);
            }

        }
        //open
        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FrmLoadProject frm = new FrmLoadProject();
            frm.ShowDialog();
            if (frm.Visible == false)
            {
                this.id = frm.projectId;
                MainFormDAO dbo = new MainFormDAO();
                dbo.GetMainFormById(frm.projectId, this);
                frm.Close();
            }
        }
        //save as
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmInputProName fipn = new FrmInputProName();
            this.id = Guid.NewGuid().ToString();
            fipn.projectName = this.projectName;
            fipn.ShowDialog();
            this.projectName = fipn.projectName;
            if (fipn.Visible == false)
            {
                fipn.Close();

                

                mdbo.SaveMainForm(this);
                    Console.WriteLine("save ok");
            }
        }

        private void freshTreeNodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNodeGenerator gen =new TreeNodeGenerator(this);
            this.treeView.Nodes.Clear();
            this.treeView.Nodes.Add(gen.GenerateMainNode());
        }

        private void objectFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ObjectModelForm modelForm = new ObjectModelForm(this);
            modelForm.Text = "ObjectModelForm";
            this.modelFormList.Add(modelForm);//添加Form对象到MainForm的对象列表modelFormList
            //parentMainForm.diagramList_List.Add(modelForm.diagramList);//添加Form对象的对象链表到MainForm的对象列表diagramList_List
            modelForm.TopLevel = false;
            this.splitContainer3.Panel2.Controls.Add(modelForm);
            modelForm.BringToFront();
            modelForm.Show();
            modelForm.Focus();
        }

        private void inforFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InformationFlowModelForm modelForm = new InformationFlowModelForm(this);
            modelForm.Text = "InformationFlowModelForm";
            this.modelFormList.Add(modelForm);//添加Form对象到MainForm的对象列表modelFormList
            //parentMainForm.diagramList_List.Add(modelForm.diagramList);//添加Form对象的对象链表到MainForm的对象列表diagramList_List
            modelForm.TopLevel = false;
            this.splitContainer3.Panel2.Controls.Add(modelForm);
            modelForm.BringToFront();
            modelForm.Show();
            modelForm.Focus();
        }

        private void stateFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StateTransitionModelForm modelForm = new StateTransitionModelForm(this);
            modelForm.Text = "StateTransitionModelForm";
            this.modelFormList.Add(modelForm);//添加Form对象到MainForm的对象列表modelFormList
            //parentMainForm.diagramList_List.Add(modelForm.diagramList);//添加Form对象的对象链表到MainForm的对象列表diagramList_List
            modelForm.TopLevel = false;
            this.splitContainer3.Panel2.Controls.Add(modelForm);
            modelForm.BringToFront();
            modelForm.Show();
            modelForm.Focus();
        }

//         private void button1_Click(object sender, EventArgs e)
//         {
//             TreeNodeGenerator gen = new TreeNodeGenerator(this);
//             this.treeView.Nodes.Clear();
//             this.treeView.Nodes.Add(gen.GenerateMainNode());
//         }

        private void button1_Click_1(object sender, EventArgs e)
        {
            TreeNodeGenerator gen = new TreeNodeGenerator(this);
            this.treeView.Nodes.Clear();
            this.treeView.Nodes.Add(gen.GenerateMainNode());
        }

        private void splitContainer3_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void splitContainer4_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void treeView_NodeMouseClick_1(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button==MouseButtons.Left)
            {
                TreeNode currentNode = e.Node;
                Object tag = currentNode.Tag;
                if (tag is OriginalModelForm)
                {
                    string id = ((OriginalModelForm)tag).id;
                    foreach (OriginalModelForm form in this.modelFormList)
                    {
                        if (id.Equals(form.id))
                        {
                            form.BringToFront();
                            break;
                        }
                    }
                } 
                else if(tag is OriginalDiagram)
                {
                    string id = ((OriginalDiagram)tag).id;
                    string parentId=((OriginalDiagram)tag).ModelForm.id;
                    foreach (OriginalModelForm form in this.modelFormList)
                    {
                        if (parentId.Equals(form.id))
                        {
                            form.BringToFront();
                            foreach (OriginalDiagram dgr in form.diagramList)
                            {
                                if (id.Equals(dgr.id))
                                {
                                    dgr.IsSelected = true;
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }

        private void getMainFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MainForm main = SystemVar.mainForm;
                Console.WriteLine();
        }

        private void writeFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
//             JavaEntityTemplate jet=new JavaEntityTemplate();
//             FileUtil.WriteFile("e:\\1.java", jet.generate());
        }

        private void openUrlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

        }

        

        

    }
}
